package com.neusoft.bizcore.auth.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.auth.dto.OAuth2ConfigDTO;
import com.neusoft.bizcore.auth.model.OAuth2Config;
import com.neusoft.bizcore.auth.repository.OAuth2ConfigRepository;
import com.neusoft.bizcore.web.dto.AbstractConverter;

@Component
public class OAuth2ConfigConverter extends AbstractConverter<OAuth2Config, OAuth2ConfigDTO> {

    @Autowired
    private OAuth2ConfigRepository oAuth2ConfigRepository;

    @Override
    public OAuth2Config toModel(final OAuth2ConfigDTO dto) {
        OAuth2Config model;
        if (dto.isNew()) {
            model = new OAuth2Config();
        } else {
            model = this.oAuth2ConfigRepository.getOne(dto.getId());
        }
        model.setName(dto.getName());
        model.setClientId(dto.getClientId());
        model.setSecretId(dto.getSecretId());
        model.setBaseUrl(dto.getBaseUrl());
        model.setAuthorizeUri(dto.getAuthorizeUri());
        model.setAccessTokenUri(dto.getAccessTokenUri());
        model.setUserInfoUri(dto.getUserInfoUri());
        model.setRedirectUrl(dto.getRedirectUrl());
        return model;
    }

    @Override
    public OAuth2ConfigDTO toDTO(final OAuth2Config model, final boolean forListView) {
        final OAuth2ConfigDTO dto = new OAuth2ConfigDTO();
        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setClientId(model.getClientId());
        dto.setSecretId(model.getSecretId());
        dto.setBaseUrl(model.getBaseUrl());
        dto.setAuthorizeUri(model.getAuthorizeUri());
        dto.setAccessTokenUri(model.getAccessTokenUri());
        dto.setUserInfoUri(model.getUserInfoUri());
        dto.setRedirectUrl(model.getRedirectUrl());
        return dto;
    }

}
